/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gov.nist.ssd.swqb.gui.components.graph;

import com.mxgraph.layout.mxCircleLayout;
import com.mxgraph.util.mxRectangle;
import com.mxgraph.view.mxGraph;
import java.awt.Dimension;

/**
 *
 * @author benjamin herbomez
 */
public class CustomCircleLayout extends mxCircleLayout {

    private Dimension size;
    private Object main = null;


    
    public CustomCircleLayout(mxGraph mxgrph, double d, Dimension s) {
        super(mxgrph, d);
        this.size = s;
    }

    public CustomCircleLayout(mxGraph mxgrph, Dimension s) {
        super(mxgrph);
        this.size = s;
    }
    
    public void setSize(Dimension size) {
        this.size = size;
    }

    public void setMain(Object main) {
        this.main = main;
    }
    
    

    @Override
    public void circle(Object[] vertices, double r, double left, double top) {
        int vertexCount = vertices.length;
        double phi = 2 * Math.PI / (vertexCount - 1 );
        double dec = Math.PI / 5;
        int k = 0;
        for (int i = 0; i < vertexCount; i++) {
            if(vertices[i] == main){
                mxRectangle vertexBounds = getVertexBounds(vertices[i]);
                float width = (float) vertexBounds.getWidth();
                float height = (float) vertexBounds.getHeight();
                setVertexLocation(vertices[i],  this.size.width*0.5  - width*0.5, 
                        this.size.height*0.5  - height*0.5);
            }
            else  {
                mxRectangle vertexBounds = getVertexBounds(vertices[i]);
                float width = (float) vertexBounds.getWidth();
                float height = (float) vertexBounds.getHeight();
                double x =this.size.width*0.5 + this.size.width * 0.4 *
                        Math.sin(k * phi + dec) - width*0.5;
                //Offset
                if(x  <  5.0)
                    x = 5.0;
                else if(x + width > size.width - 5)
                    x = size.width - 5 - width;
                
                double y = this.size.height*0.5 + this.size.height * 0.4
                        * Math.cos(k * phi + dec) - height*0.5;
                
                setVertexLocation(vertices[i],  x,y);
                ++k;
            }
        }
    }
}
